/*!
 * Copyright (c) 2023-present StarDreamNet
 */

(document.loader || (document.loader = [])).push(["home", class {
    constructor() {
        this.postData = null;
        this.newThings = null;
    };
    ourPostsloadStart() {
        $(".app-cards").attr("load", "").html("").append($("<p class='load'>加载中</p>"));
        this.resize();
    };
    ourPostsloadEnd() {
        $(".app-cards").removeAttr("load", "").find("p.load").remove();
        this.resize();
    };
    ourPostsloadError() {
        $(".app-cards").attr("load", "").attr("error", "").html("<p>加载出错</p>");
        this.resize();
    };
    loadOurPosts() {
        var data = this.postData;
        var cards = $('.app-cards');
        this.loadPostsByData(data, cards);
        this.resize();
    };
    newThingsloadStart() {
        $(".app-main-begin-newthing-content").attr("load", "").html("").append($("<p class='load'>加载中</p>"));
        this.resize();
    };
    newThingsloadEnd() {
        $(".app-main-begin-newthing-content").removeAttr("load", "").find("p.load").remove();
        this.resize();
    };
    newThingsloadError() {
        $(".app-main-begin-newthing-content").attr("load", "").attr("error", "").html("<p>加载出错</p>");
        this.resize();
    };
    loadNewThings() {
        for (let val of this.newThings) {
            let a = $("<a></a>");
            a.text(val.name);
            a.attr("href", val.url);
            a.attr("alt", val.name);
            $('.app-main-begin-newthing-content').append(a);
        }
    };
    loadAll() {
        var th = this;
        th.ourPostsloadStart();
        $.get('/api/shequ/get_home_post_list?t=' + Date.now()).done(function (responseText) {
            var data = responseText;
            th.postData = data.data;

            th.ourPostsloadEnd();
            th.loadOurPosts()
        }).fail(function (error) {
            th.ourPostsloadError();
        });

        th.newThingsloadStart();
        $.get('/api/get_new_things?t=' + Date.now()).done(function (responseText) {
            var data = responseText;
            th.newThings = data.data;

            th.newThingsloadEnd();
            th.loadNewThings();
        }).fail(function (error) {
            th.newThingsloadError();
        });
    };
    init_(require) {
        const { resize, dateSpanString } = require("index");
        const { loadPostsByData } = require("postlist");

        this.resize = resize;
        this.dateSpanString = dateSpanString;
        this.loadPostsByData = loadPostsByData;

        this.loadAll();
    }
}])